package great.project.common.utils;

import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Utils {
 
    /**
     * 测试编码  OK
     */
    public static void main(String[] args) throws Throwable {
        System.out.println("*************************方式一*********************");
        String encode1 = Base64Encode("在线加密解密测试", "UTF-8");
        System.out.println("Encode1 " + encode1);
        String decode1 = Base64Decode(encode1, "UTF-8");
        System.out.println("decode1 " + decode1);
        System.out.println("*************************方式二*********************");
        String encode2 = encode2("在线加密解密测试", "UTF-8");
        System.out.println("encode2 "+encode2);
        String decode2 = decode2(encode2, "UTF-8");
        System.out.println("decode2 "+decode2);
        System.out.println("*************************方式三*********************");
//        String encode3 = encode3("在线加密解密测试", "UTF-8");
//        System.out.println("encode3 "+encode3);
//        String decode3 = decode3(encode3, "UTF-8");
//        System.out.println("decode3 "+decode3);
        System.out.println("*************************方式四*********************");
        String encode4 = encode4("在线加密解密测试", "UTF-8");
        System.out.println("encode4 "+encode4);
//        String decode4 = decode3(encode4, "UTF-8");
//        System.out.println("decode4 "+decode4);
    }
 
    /*****************************************方式一**********************************************/
    /***************使用jdk自带的DatatypeConverter.java类实现，但是jdk版本必须>=1.6。****************/
 
    /**
     * base64编码
     *
     * @param result
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String Base64Encode(String result, String encodeType) throws Throwable {
        try {
            byte[] data = result.getBytes(encodeType);
            return DatatypeConverter.printBase64Binary(data);
        } catch (Exception e) {
            throw new Throwable("base64编码错误");
        }
    }
 
 
    /**
     * base64解码
     *
     * @param encodeType
     * @param result
     * @return
     */
    public static String Base64Decode(String result, String encodeType) throws Throwable {
        try {
            byte[] data = DatatypeConverter.parseBase64Binary(result);
            return new String(data, encodeType);
        } catch (Exception e) {
            throw new Throwable("base64解码");
        }
    }
 
    /*****************************************方式二**********************************************/
    /*******************使用jdk自带的Base64.java类实现，但是jdk版本必须>=1.8。***********************/
 
    /**
     * base64编码
     *
     * @param str
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode2(String str, String encodeType) throws UnsupportedEncodingException {
        byte[] data = str.getBytes(encodeType);
        return Base64.getEncoder().encodeToString(data);
    }
 
    /**
     * base64解码
     *
     * @param result
     * @param encodeType
     * @return
     */
    public static String decode2(String result, String encodeType) throws UnsupportedEncodingException {
        byte[] data = result.getBytes(encodeType);
        byte[] decode = Base64.getDecoder().decode(data);
        return new String(decode,encodeType);
    }
 
    /*****************************************方式三**********************************************/
    /** 使用cn.hutool.com.example.hot_deployment.core.codec.Base64类实现
     * <dependency>
     *     <groupId>cn.hutool</groupId>
     *     <artifactId>hutool-all</artifactId>
     *     <version>4.5.0</version>
     * </dependency>
     */
 
    /**
     * base64编码
     *
     * @param str
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
   // public static String encode3(String str, String encodeType) throws UnsupportedEncodingException {
   //     return cn.hutool.com.example.hot_deployment.core.codec.Base64.encode(str,encodeType);
   // }
 
    /**
     * base64解码
     *
     * @param result
     * @param encodeType
     * @return
     */
  //  public static String decode3(String result, String encodeType) throws UnsupportedEncodingException {
  //      return cn.hutool.com.example.hot_deployment.core.codec.Base64.decodeStr(result,encodeType);
  //  }
 
    /*****************************************方式四**********************************************/
    /** 使用org.apache.commons.codec.binary.Base64类实现
     * <dependency>
     *     <groupId>commons-codec</groupId>
     *     <artifactId>commons-codec</artifactId>
     *     <version>1.11</version>
     * </dependency>
     */
 
    /**
     * base64编码
     *
     * @param str
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode4(String str, String encodeType) throws UnsupportedEncodingException {
        return org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes(encodeType));
    }
 
    /**
     * base64解码
     *
     * @param result
     * @param encodeType
     * @return
     */
    public static String decode4(String result, String encodeType) throws UnsupportedEncodingException {
        return new String(org.apache.commons.codec.binary.Base64.decodeBase64(result),encodeType);
    }
 
}